# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=libxc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=6.2.2
pkgrel=4
pkgdesc="Library of exchange-correlation functionals for density-functional theory. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://libxc.gitlab.io'
msys2_repository_url="https://gitlab.com/libxc/libxc"
msys2_references=(
  'aur: libxc'
)
license=('spdx:MPL-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-numpy: Python bindings")
if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
  optdepends+=("${MINGW_PACKAGE_PREFIX}-gcc-libgfortran: Fortran bindings")
fi
source=("https://gitlab.com/libxc/libxc/-/archive/${pkgver}/${_realname}-${pkgver}.tar.bz2"
        "001-fix-pkgconfig-files.patch"
        "002-fix-cmake-config-files.patch")
sha256sums=('f72ed08af7b9dff5f57482c5f97bff22c7dc49da9564bc93871997cbda6dacf3'
            'dddd21a3514f8483072d6cc41f846979642441423da7c8f976dafd8d8ebad64a'
            '046567024c6977949cc303aaa04bebd203c7ba1feb37a4e87730f4ea22a4eaa3')
noextract=("${_realname}-${pkgver}.tar.bz2")

prepare() {
  echo "Extracting ${_realname}-${pkgver}.tar.bz2..."
  tar -xjf ${_realname}-${pkgver}.tar.bz2 || true

  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-pkgconfig-files.patch
  patch -p1 -i "${srcdir}"/002-fix-cmake-config-files.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DNAMESPACE_INSTALL_INCLUDEDIR=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DNAMESPACE_INSTALL_INCLUDEDIR="/libxc" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DENABLE_GENERIC=ON \
      -DENABLE_FORTRAN=ON \
      -DENABLE_PYTHON=ON \
      -DENABLE_XHOST=OFF \
      -DBUILD_TESTING=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING

  ${MINGW_PREFIX}/bin/python -m compileall -o 0 -o 1 -q "${pkgdir}"${MINGW_PREFIX}
}
